home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / DClap / DMenu.cpp < prev    next >
Text File  |  1996-07-05  |  10KB  |  428 lines

  1. // DMenu.cp
  2. // d.g.gilbert
  3.  
  4.  
  5. #include "Dvibrant.h"
  6. #include "DMenu.h"
  7. #include "DViewCentral.h"
  8. #include "DApplication.h"
  9. #include "DUtil.h"
  10.  
  11.  
  12.  
  13. extern "C" char * kMenuCommandFlag; // from vibrant's vibmenus.c == "%$%"; 
  14.  
  15.  
  16. //class DMenuChoiceGroup
  17.  
  18. extern "C" void ChoiceGroupHandler(Nlm_ChoicE c)
  19. {
  20.     DMenuChoiceGroup *obj= (DMenuChoiceGroup*) Nlm_GetObject( (Nlm_GraphiC)c);
  21.     if (obj) obj->IsMyViewAction(obj);
  22. }
  23.  
  24. DMenuChoiceGroup::DMenuChoiceGroup(long id, DMenu* supermenu) :
  25.     DView( id, NULL, kMenu, supermenu),
  26.     fLastItem(NULL)
  27. {
  28.     fChoice = Nlm_ChoiceGroup(supermenu->fMenu, ChoiceGroupHandler);
  29.     fSupermenu= supermenu;
  30.     this->SetNlmObject(fChoice);
  31. }
  32.     
  33. DMenuChoiceGroup::DMenuChoiceGroup(long id, DMenu* supermenu, Nlm_ChoicE itsChoice) :
  34.     DView( id, NULL, kMenu, supermenu),
  35.     fChoice(itsChoice)
  36. {
  37.     fSupermenu= supermenu;
  38.     this->SetNlmObject(fChoice);
  39. }
  40.  
  41. void  DMenuChoiceGroup::AddItem( long id, char* title, Boolean hasCmdKeys) 
  42. {
  43.     if (hasCmdKeys) { 
  44.         char stemp[256];
  45.         StrCpy( stemp, kMenuCommandFlag);
  46.         StrNCat( stemp, title, sizeof(stemp));
  47.         fLastItem= Nlm_ChoiceItem(fChoice, stemp);
  48.         }
  49.     else
  50.         fLastItem= Nlm_ChoiceItem(fChoice, title);
  51.     //Nlm_Disable(item); // not til debug
  52.     gViewCentral->RegisterView(id, fLastItem, kMenuItem, this); 
  53. }
  54.  
  55.  
  56.  
  57. // class DMenu  
  58.  
  59.  
  60.  
  61. // vibrant callback handler
  62.  
  63. extern "C" void MenuItemHandler(Nlm_IteM    item)
  64. {
  65.     DMenu *obj= (DMenu*) Nlm_GetObject( (Nlm_GraphiC)item);
  66.     if (obj)  obj->IsMyViewAction(obj);
  67. }
  68.  
  69.  
  70. DMenu::DMenu(long id, Nlm_MenU itsMenu, DTaskMaster* itsSuperior) :
  71.         DView(id, itsMenu, kMenu, itsSuperior),
  72.         fFonts(NULL),
  73.         fMenu(itsMenu) 
  74. {
  75.     //fItemList= new DList(NULL, DList::kDeleteObjects);
  76.     fItemList= new DList(NULL);
  77. }
  78.  
  79. DMenu::~DMenu()
  80. {
  81.     // !? ~DList is not calling obj destructors for kDeleteObjects !?!?
  82.     short i, n= fItemList->GetSize();
  83.     for (i=0; i<n; i++) {
  84.         DString* st= (DString*) fItemList->At(i);
  85.         delete st;
  86.         }
  87.     delete fItemList;
  88. }
  89.  
  90.  
  91. void  DMenu::AddItem(long id, char* title, Boolean isCheckItem, Boolean hasCmdKeys) 
  92. {
  93.     Nlm_IteM item;
  94.     if (hasCmdKeys) { 
  95.         char stemp[256];
  96.         StrCpy( stemp, kMenuCommandFlag);
  97.         StrNCat( stemp, title, sizeof(stemp));
  98.         if (isCheckItem) 
  99.             item= Nlm_StatusItem(fMenu, stemp, MenuItemHandler);
  100.         else
  101.             item= Nlm_CommandItem(fMenu, stemp, MenuItemHandler);
  102.         }
  103.     else {
  104.         if (isCheckItem) 
  105.             item= Nlm_StatusItem(fMenu, title, MenuItemHandler);
  106.         else
  107.             item= Nlm_CommandItem(fMenu, title, MenuItemHandler);
  108.         }
  109.         
  110.     // need our own copy of titles as XMotif/Vibrant has no callback to get them !
  111.     DString* s= new DString(title);
  112.     fItemList->InsertLast(s);
  113.             
  114.             // SOMETIME, REVISE this method to return itemview as result !!!
  115.             
  116.     DView* itemview= new DView(id, item, kMenuItem, this);
  117.     itemview->SetNlmObject(item);
  118.     //Nlm_Disable(item); // not till we debug...
  119.     if (itemview->GetOwnerCount()>1) itemview->suicide(); // leave gViewCentral as owner...
  120. }
  121.  
  122.  
  123. void DMenu::SetItemStatus(short itemid, Boolean status) 
  124. {  
  125.     DView* anItem;
  126.     anItem= this->FindSubview(itemid);
  127.     if (anItem) anItem->SetStatus(status);
  128. }            
  129.  
  130. Boolean DMenu::GetItemStatus(short itemid) 
  131. {  
  132.     DView* anItem;
  133.     anItem= this->FindSubview(itemid);
  134.     if (anItem && anItem->GetStatus()) return true;
  135.     else return false;
  136. }            
  137.  
  138. #if 0            
  139.         // ?? redo thise as above ??
  140.     virtual void SetItemTitle(short item, char* title);    
  141.     virtual char* GetItemTitle(short item, char* title = NULL, size_t maxsize = 256);
  142. #endif
  143.  
  144.  
  145.  
  146. char* DMenu::GetItemTitle(short item, char* title, ulong maxsize)
  147. {
  148.     if (item>0) {
  149.       DString* s= (DString*) fItemList->At(item-1);
  150.       if (s) {
  151.         char *cp= (char*) s->Get();
  152.         if (title==NULL) {
  153.           maxsize= StrLen(cp) + 1;
  154.           title= (char*) MemNew(maxsize);
  155.           }
  156.         StrNCpy(title, cp, maxsize);
  157.         }
  158.     }
  159.     return title;
  160. }
  161.  
  162. void  DMenu::AddSeparator(void) 
  163. {
  164.     DString* s= new DString("-");
  165.     fItemList->InsertLast(s);
  166.     Nlm_SeparatorItem(fMenu);
  167. }
  168.  
  169.  
  170. static Nlm_VGphIntChrSiz    gGetFontTitle = NULL;  // need special vibrant callback
  171.  
  172. void  DMenu::AddFonts(void) 
  173. {
  174. #ifdef WIN_MOTIF
  175.     fFonts= new DMenuChoiceGroup(cFontGroup, this);
  176.     fFonts->AddItem(cFontGroup+1,"Courier");
  177.     fFonts->AddItem(cFontGroup+2,"Helvetica");
  178.     fFonts->AddItem(cFontGroup+3,"Times");
  179. #endif
  180. #ifdef WIN_MSWIN
  181.     fFonts= new DMenuChoiceGroup(cFontGroup, this);
  182.     fFonts->AddItem(cFontGroup+1,"Arial");
  183.     fFonts->AddItem(cFontGroup+2,"Courier");
  184.     fFonts->AddItem(cFontGroup+3,"Times");
  185.     fFonts->AddItem(cFontGroup+4,"Modern");
  186.     fFonts->AddItem(cFontGroup+5,"Roman");
  187.     fFonts->AddItem(cFontGroup+6,"Script");
  188.     fFonts->AddItem(cFontGroup+7,"Serife");
  189. #endif
  190. #ifdef WIN_MAC
  191.     Nlm_ChoicE theFonts= Nlm_FontGroup(fMenu);
  192.     fFonts= new DMenuChoiceGroup(cFontGroup, this, theFonts);
  193. #endif
  194.     fFonts->AddItem(cFontGroup+20,"System");
  195.     fFonts->AddItem(cFontGroup+21,"Program");
  196. }
  197.  
  198.  
  199. void  DMenu::SetFontChoice(char* name) 
  200. {
  201.     char  namebuf[256];
  202.     short item, nitems;
  203.     
  204.     *namebuf= 0;
  205.     if (name && fFonts) { //&& gGetFontTitle) {
  206.         nitems= this->CountItems();
  207.         for (item= 1; item<= nitems; item++) {
  208.             Nlm_GetChoiceTitle((Nlm_GraphiC)fFonts->fNlmObject, item, namebuf, 256);
  209.             if (StringCmp(name, namebuf)==0)  {
  210.                 fFonts->SetValue(item); // is this it?
  211.                 return;
  212.                 }
  213.             }
  214.         }
  215. }
  216.  
  217.  
  218. char*  DMenu::GetFontChoice(char* namebuf, ulong bufsize)
  219. {
  220.     if (namebuf && bufsize) *namebuf= 0;
  221.     if (fFonts) { // && gGetFontTitle) {
  222.         short item= fFonts->GetValue();  // this is correct
  223.         if (!namebuf) {
  224.              bufsize= 256;
  225.             namebuf= (char*)MemNew(bufsize);
  226.             }
  227.         Nlm_GetChoiceTitle((Nlm_GraphiC)fFonts->fNlmObject, item, namebuf, bufsize);
  228.         return namebuf;  
  229.         }
  230.     else
  231.         return NULL;
  232. }
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240. //class DPulldownMenu : public DMenu
  241.  
  242. DPulldownMenu::DPulldownMenu(long id, DTaskMaster* itsSuperior, Nlm_WindoW w, char* title):
  243.     DMenu(id, Nlm_PulldownMenu(w, title), itsSuperior)
  244.     //fMenu= Nlm_PulldownMenu(w, title);
  245.     //this->SetNlmObject(fMenu);
  246.  
  247.  
  248.  
  249. //class DAppleMenu : public DMenu
  250.  
  251. DAppleMenu::DAppleMenu(DTaskMaster* itsSuperior, char* aboutName):
  252.     DMenu(kAboutMenu, Nlm_AppleMenu(NULL), itsSuperior)
  253. {
  254.     //fMenu= Nlm_AppleMenu(NULL);
  255.     //this->SetNlmObject(fMenu);
  256.     this->AddItem( kAboutMenuItem, aboutName);
  257.     this->AddSeparator();
  258.     Nlm_DeskAccGroup(fMenu);
  259. }
  260.  
  261.  
  262.  
  263. Boolean DAppleMenu::IsMyAction(DTaskMaster* theView) // override
  264. {
  265.     if (theView->Id() == kAboutMenuItem) {
  266.         gApplication->DoAboutBox();
  267.         return true;
  268.         }
  269.     else
  270.         return DView::IsMyAction(theView);
  271. }
  272.  
  273.  
  274. //class DSubMenu : public DMenu
  275.  
  276. DSubMenu::DSubMenu(long id, DMenu* supermenu, char* title) :
  277.     DMenu(id, Nlm_SubMenu(supermenu->fMenu, title), supermenu) 
  278. {
  279.     //fMenu= Nlm_SubMenu(supermenu->fMenu, title);
  280.     //this->SetNlmObject(fMenu);
  281. }
  282.  
  283.  
  284. //class DPopupMenu : public DMenu
  285.  
  286. //#ifdef WIN_MOTIF
  287. #ifndef WIN_MAC
  288.  
  289.  
  290. // this works for motif (and other form DOESN'T work for Motif...)
  291. // but this fails for Mac (at least names of items are not displayed... fiddle w/ it)
  292. // works now for mswin 
  293.  
  294. extern "C" void PopupMenuHandler(Nlm_PopuP p)
  295. {
  296.     DPopupMenu *obj= (DPopupMenu*) Nlm_GetObject((Nlm_GraphiC)p);
  297.     if (obj) obj->IsMyViewAction(obj);
  298. }
  299.  
  300.  
  301. DPopupMenu::DPopupMenu(long id, Nlm_GrouP prnt, char* title) :
  302.     DMenu(id, NULL, NULL)
  303. {
  304.         //fMenu= Nlm_PopupMenu(prnt, title);
  305.         // try it w/ crazee popuplist...
  306.         // really want to have a DPopupList class that includes all the DMenu methods...
  307.     
  308.     Nlm_RecT r;
  309.     fPopup= Nlm_PopupList( prnt, true, PopupMenuHandler); // true==maclike
  310.     fMenu= (Nlm_MenU) Nlm_Parent(fPopup);
  311.     this->SetNlmObject(fMenu);
  312. }
  313.  
  314. void  DPopupMenu::AddFonts()
  315. {
  316.     // strictly a hack -- we need to clean up this/DMenu font stuff
  317. #ifdef WIN_MOTIF
  318.     Nlm_PopupItem(fPopup, "Courier");
  319.     Nlm_PopupItem(fPopup, "Helvetica");
  320.     Nlm_PopupItem(fPopup, "Times");
  321.     Nlm_PopupItem(fPopup, "Program");
  322.     Nlm_PopupItem(fPopup, "System");
  323. #endif
  324. #ifdef WIN_MSWIN
  325.     Nlm_PopupItem(fPopup, "Arial");
  326.     Nlm_PopupItem(fPopup, "Courier");
  327.     Nlm_PopupItem(fPopup, "Modern");
  328.     Nlm_PopupItem(fPopup, "Roman");
  329.     Nlm_PopupItem(fPopup, "Times");
  330.     Nlm_PopupItem(fPopup, "Script");
  331.     Nlm_PopupItem(fPopup, "Serife");
  332.     Nlm_PopupItem(fPopup, "Program");
  333.     Nlm_PopupItem(fPopup, "System");
  334. #endif
  335. #ifdef WIN_MAC
  336.     Nlm_IteM item;
  337.     Nlm_ChoicE theFonts= Nlm_FontGroup(fMenu);
  338.     item= Nlm_ChoiceItem(theFonts, "Program");
  339.     item= Nlm_ChoiceItem(theFonts, "System");
  340.     fFonts= new DMenuChoiceGroup(cFontGroup, this, theFonts);
  341. #endif
  342. }
  343.  
  344. void  DPopupMenu::AddItem(long id, char* title, Boolean isCheckItem, Boolean hasCmdKeys)
  345. {
  346.     Nlm_IteM item;
  347.     Nlm_PopupItem(fPopup, title);
  348.  
  349.     // need our own copy of titles as XMotif/Vibrant has no callback to get them !
  350.     DString* s= new DString(title);
  351.     fItemList->InsertLast(s);
  352.  
  353.             // SOMETIME, REVISE this method to return itemview as result !!!
  354.     //DView* itemview= new DView(id, item, kMenuItem, this);
  355.     //itemview->SetNlmObject(item);
  356.     //if (itemview->GetOwnerCount()>1) itemview->suicide(); // leave gViewCentral as owner...
  357. }
  358.  
  359.  
  360. void  DPopupMenu::SetFontChoice(char* name)
  361. {
  362.     char  namebuf[256];
  363.     short item, nitems;
  364.  
  365.     *namebuf= 0;
  366.     if (name) {
  367.         nitems= this->CountItems();
  368.         for (item= 1; item<= nitems; item++) {
  369.             Nlm_GetItemTitle( fPopup, item, namebuf, 256);
  370.             if (StringCmp(name, namebuf)==0)  {
  371.                 Nlm_SetValue(fPopup, item);
  372.                 return;
  373.                 }
  374.             }
  375.         }
  376. }
  377.  
  378. char*  DPopupMenu::GetFontChoice(char* namebuf, ulong bufsize)
  379. {
  380.     if (namebuf && bufsize) *namebuf= 0;
  381.     if (fPopup) {
  382.         short item= Nlm_GetValue(fPopup);
  383.         if (!namebuf) {
  384.             bufsize= 256;
  385.             namebuf= (char*)MemNew(bufsize);
  386.             }
  387.         Nlm_GetItemTitle( fPopup, item, namebuf, bufsize);
  388.         return namebuf;
  389.         }
  390.     else
  391.         return NULL;
  392. }
  393.  
  394. #else
  395.  
  396.     // this stupid old Nlm_PopupMenu() has no callback to GetItemTitle() for items !!
  397. DPopupMenu::DPopupMenu(long id, Nlm_GrouP prnt, char* title) :
  398.     DMenu(id, Nlm_PopupMenu(prnt, title), NULL)
  399. {
  400.     //fMenu= Nlm_PopupMenu(prnt, title);
  401.     //this->SetNlmObject(fMenu);
  402. }
  403.  
  404. void  DPopupMenu::AddFonts()
  405. {
  406.     DMenu::AddFonts();
  407. }
  408.  
  409. void  DPopupMenu::AddItem(long id, char* title, Boolean isCheckItem, Boolean hasCmdKeys)
  410. {
  411.     DMenu::AddItem(id,title,isCheckItem,hasCmdKeys);
  412. }
  413.  
  414. void  DPopupMenu::SetFontChoice(char* name)
  415. {
  416.     DMenu::SetFontChoice(name);
  417. }
  418.  
  419. char*  DPopupMenu::GetFontChoice(char* namebuf, ulong bufsize)
  420. {
  421.     return DMenu::GetFontChoice(namebuf, bufsize);
  422. }
  423.  
  424. #endif
  425.  
  426.